﻿using System.IO;
using System.Windows.Forms;

namespace XmlParser
{
    class Utility
    {
        public static bool WriteFile(string path, bool IsRelative, string FileName, string Data)
        {
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            path = Path.Combine(path, FileName);
            if (DeleteFile(path, IsRelative, FileName))
            {
                //path = IsRelative ? Application.StartupPath + path : path;
                
                FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter Writer = new StreamWriter(fileStream);
                Writer.Write(Data);
                Writer.Flush();
                Writer.Close();
                return true;
            }
            else
                return false;
        }

        public static bool DeleteFile(string Path, bool IsRelative, string FileName)
        {
            try
            {
                FileInfo fi = new FileInfo(Path);
                if (fi.Exists)
                    fi.Delete();
                return true;
            }
            catch 
            {
                return false;
            }
        }

    }
}
